﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Runtime.Serialization;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Fixx.DTO;

namespace Fixx.Connector
{
	public class FixxPersistantStorageBinder : SerializationBinder
	{

		public override Type BindToType(string assemblyName,
										   string typeName)
		{
			string[] typeInfo = typeName.Split('.');

			string className = typeInfo[typeInfo.Length - 1];
			if (className.Equals("FixxRepository"))
				return typeof(FixxRepository);
			if (className.Equals("FixxArea"))
				return typeof(FixxArea);
			if (className.Equals("FixxIssueType"))
				return typeof(FixxIssueType);
			if (className.Equals("FixxPriority"))
				return typeof(FixxPriority);
			if (className.Equals("FixxProject"))
				return typeof(FixxProject);
			if (className.Equals("FixxResolution"))
				return typeof(FixxResolution);
			if (className.Equals("FixxUser"))
				return typeof(FixxUser);
			if (className.Equals("FixxVersion"))
				return typeof(FixxVersion);
			if (className.Equals("FixxSettings"))
				return typeof(FixxSettings);

			else
			{
				return Type.GetType(string.Format("{0}, {1}",
								   typeName, assemblyName));
			}
		}
	}


	[Serializable]
	public class FixxPersistantStorage
	{
		private string settingsFileName = "FixxPersistantStorage.dat";
		
		public FixxRepository Repository { get; set; }

		public FixxPersistantStorage()
		{
			LoadData();
		}

		protected FixxPersistantStorage(SerializationInfo info, StreamingContext context)
		{
			this.Repository = (FixxRepository)info.GetValue("Repository", typeof(FixxRepository));
		}
		public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
		{
			info.AddValue("Repository", this.Repository);
		}

		private void LoadData()
		{
			IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User
						| IsolatedStorageScope.Assembly, null, null);
			if (isoStore.GetFileNames(settingsFileName).Length == 0)
			{
				return;
			}
			Stream stream = new IsolatedStorageFileStream(settingsFileName,
						FileMode.OpenOrCreate, isoStore);
			if (stream != null)
			{
				try
				{
					IFormatter formatter = new BinaryFormatter();
					formatter.Binder = new FixxPersistantStorageBinder();
					FixxPersistantStorage appData = (FixxPersistantStorage)formatter.Deserialize(stream);
					this.Repository = appData.Repository;
					this.Repository.OnCacheStateChange += this.OnCacheStateChange;

				}
				catch (Exception e)
				{

				}
				finally
				{
					stream.Close();
				}
			}
		}

		public void ReLoad()
		{
			LoadData();
		}
		public void OnCacheStateChange(object sender, EventArgs e)
		{
			this.Save();
		}
		public void Save()
		{
			IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User
						| IsolatedStorageScope.Assembly, null, null);
			Stream stream = new IsolatedStorageFileStream(settingsFileName,
						FileMode.Create, isoStore);

			if (stream != null)
			{
				try
				{
					IFormatter formatter = new BinaryFormatter();
					formatter.Binder = new FixxPersistantStorageBinder();
					formatter.Serialize(stream, this);
				}
				catch (Exception e)
				{

				}
				finally
				{
					stream.Close();
				}
			}
		}
	}
}
